import Vue from "vue";
import logger from "@/components/log";
import serviceCenter, { commandTypes } from "./services";
import commandCenter from "./command";
import { fail } from "./services/base-service";
export const event = new Vue({
    name: "global-event-bus",
    created() {
        logger.info("注册事件中心成功");
        this.$on("command", ({ commandStr, callback }) => {
            // 解析命令, 返回结果, 扮演Controller角色
            const { matched, command, options } =
                commandCenter.parse(commandStr);
            if (!matched) {
                return callback(fail(`不支持的命令: ${commandStr}`));
            }
            // help走绿色通道, 无需经过解析
            if (commandStr.indexOf("help") !== -1) {
                options["help"] = true;
            }
            serviceCenter
                .execute(command, options)
                .then(callback)
                .catch((errorMsg) => {
                    return callback(fail(errorMsg));
                });
        });
    },
});
